local items = {
	settingsfile = "335痛苦术.json",
    callback = GUICallback,
	{ type = "title", text = "335痛苦术 |c0000CED1糊涂虫" },
	{ type = "separator" },
	{ type = "title", text = "|cffFFFF00基础模块" },
    { type = "entry", text = "\124T"..GetItemIcon(33312)..":26:26\124t T9装备ID", tooltip = "战斗前替换设置的2件T9装备ID，施放火盾，提高暴击", enabled = true, value = "47753,47755", width = 100, key = "EquipId" },
    { type = "entry", text = "\124T"..GetItemIcon(33312)..":26:26\124t 使用法力宝石 < MP%", tooltip = "使用法力宝石 < MP%", enabled = true, value = 70, key = "ManaGem" },
	{ type = "entry", text = "\124T"..select(3, GetSpellInfo(12051))..":26:26\124t 唤醒 < MP%", tooltip = "唤醒 < MP%", enabled = true, value = 10, key = "Evocation" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(54999))..":26:26\124t 调试模式", tooltip = "开启后打印施法信息", enabled = false, key = "Debug" },

    { type = "separator" },
	{ type = "title", text = "|cffFFFF00辅助功能" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(1953))..":26:26\124t 释放闪现", tooltip = "按住左Shift施放闪现", enabled = true, key = "Blink" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(45438))..":26:26\124t 释放冰箱 < HP%", tooltip = "按住右Shift主动施放冰箱，关闭后无法自动开启", enabled = true, value = 10, key = "IceBlock" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(54999))..":26:26\124t 工程手套 < 码", tooltip = "工程手套CD好了就用", enabled = true, value = 41, key = "UseHand" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(55016))..":26:26\124t 工程鞋子（右Ctrl）", tooltip = "按住右Ctrl施放工程疾跑", enabled = true, key = "UseFoot" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(475))..":26:26\124t 自动解诅咒", tooltip = "开启自动解诅咒功能", enabled = false, key = "RemoveCurse" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(30449))..":26:26\124t 自动偷魔法", tooltip = "开启后进行自动偷取魔法", enabled = false, key = "Spellsteal" },
	
    { type = "separator" },
	{ type = "title", text = "|cffFFFF00保命" },
    { type = "entry", text = "\124T"..GetItemIcon(36894)..":26:26\124t  治疗石 < HP% ", tooltip = "治疗石 < HP%", enabled = true, value = 20, key = "FelHealthstone" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(1463))..":26:26\124t 释放法力护盾 < HP%", tooltip = "开启法力护盾 < HP%", enabled = true, value = 30, key = "ManaShield" },

    { type = "separator" },
	{ type = "title", text = "|cffFFFF00 左爆发（按住左Ctrl）" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(12042))..":26:26\124t 开启自动爆发", tooltip = "英勇、嗜血、能量灌注的时候自动开启爆发", enabled = true, key = "AutoBurst" },
    { type = "separator" },
	{ type = "title", text = "|cffFFFF00炎爆术" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(42891))..":26:26\124t 释放炎爆术", tooltip = "这里设置炎爆术是为了提高技能优先级", enabled = true, key = "Pyroblast" },

    { type = "separator" },
	{ type = "title", text = "|cffFFFF00 群体AOE输出（左暴风Alt右烈焰）" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(10))..":26:26\124t 自动释放暴风雪 > 怪物数", tooltip = "自动释放暴风雪 > 怪物数", enabled = true, value = 3, key = "BlizzardCount" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(42926))..":26:26\124t 自动释放烈焰风暴 > 怪物数", tooltip = "自动释放烈焰风暴 > 怪物数", enabled = true, value = 3, key = "FlamestrikeCount" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(55360))..":26:26\124t 自动活动炸弹 < 施法距离", tooltip = "施法范围内目标全部释放活动炸弹", enabled = true, value = 41, min = 36, max = 41, step = 1, key = "LivingBomb" },

    { type = "separator" },
	{ type = "title", text = "|cffFFFF00折磨火输出" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(2948))..":26:26\124t 灼烧", tooltip = "补充5%暴击使用", enabled = false, key = "Scorch" },
    { type = "entry", text = "\124T"..select(3, GetSpellInfo(11129))..":26:26\124t 折磨火输出循环", tooltip = "折磨火输出循环", enabled = true, key = "FireDps" },
};


local function GetSetting(name)
    for k, v in ipairs(items) do
        if v.type == "entry"
         and v.key ~= nil
         and v.key == name then
            return v.value, v.enabled
        end
    end
end;

local function OnLoad()
    ni.GUI.AddFrame("335痛苦术-糊涂虫", items);
end

local function OnUnLoad()  
	ni.GUI.DestroyFrame("335痛苦术-糊涂虫");
end


local spells = {
	ShadowBolt = {id = 47809, name = GetSpellInfo(47809), tootip = "暗影箭"}, --暗影箭
    FelArmor = {id = 47893, name = GetSpellInfo(47893), tootip = "邪甲术"}, --邪甲术 
    CreateHealthstone = {id = 47878, name = GetSpellInfo(47878), tootip = "制作治疗石"},  --制作治疗石
	FelHealthstone = {id = 36894, name = GetItemInfo(36894), tootip = "邪能治疗石"}, --邪能治疗石
    CreateSpellstone = {id = 47888, name = GetSpellInfo(47888), tootip = "制作法术石"},  --制作法术石
    GrandSpellstone = {id = 41196, name = GetItemInfo(41196), tootip = "完美法术石"}, --完美法术石 
    CreateSoulstone = {id = 47884, name = GetSpellInfo(47884), tootip = "制作灵魂石"},  --制作灵魂石
    DemonicSoulstone = {id = 36895, name = GetItemInfo(36895), tootip = "恶魔灵魂石"}, --恶魔灵魂石
    FelDomination = {id = 18708, name = GetSpellInfo(18708), tootip = "恶魔支配"}, --恶魔支配
	DemonicEmpowerment = {id = 47193, name = GetSpellInfo(47193), tootip = "恶魔增效"},  --恶魔增效
    ShadowCleave = {id = 50581, name = GetSpellInfo(50581), tootip = "暗影顺劈"},  --暗影顺劈
    ImmolationAura = {id = 50589, name = GetSpellInfo(50589), tootip = "献祭光环"},  --献祭光环
    DemonCharge = {id = 54785, name = GetSpellInfo(54785), tootip = "恶魔冲锋"},  --恶魔冲锋
    Metamorphosis = {id = 47241, name = GetSpellInfo(47241), tootip = "恶魔变形"},  --恶魔变形
    SummonFelguard = {id = 30146, name = GetSpellInfo(30146), tootip = "召唤恶魔卫士"},  --召唤恶魔卫士
    Shadowflame = {id = 61290, name = GetSpellInfo(61290), tootip = "暗影烈焰"},  --暗影烈焰
    RainofFire = {id = 47820, name = GetSpellInfo(47820), tootip = "火焰之雨"},  --火焰之雨
    SoulFire = {id = 47825, name = GetSpellInfo(47825), tootip = "灵魂之火"},  --灵魂之火
    Incinerate = {id = 47838, name = GetSpellInfo(47838), tootip = "烧尽"},  --烧尽
    LifeTap = {id = 57946, name = GetSpellInfo(57946), tootip = "生命分流"},  --生命分流
    DeathCoil = {id = 47860, name = GetSpellInfo(47860), tootip = "死亡缠绕"},  --死亡缠绕
    CurseofDoom = {id = 47867, name = GetSpellInfo(47867), tootip = "厄运诅咒"},  --厄运诅咒
    CurseofAgony = {id = 47864, name = GetSpellInfo(47864), tootip = "痛苦诅咒"},  --痛苦诅咒
	CurseoftheElements = {id = 47865, name = GetSpellInfo(47865), tootip = "元素诅咒"},  --元素诅咒
    SeedofCorruption = {id = 47836, name = GetSpellInfo(47836), tootip = "腐蚀之种"},  --腐蚀之种
    Corruption = {id = 47813, name = GetSpellInfo(47813), tootip = "腐蚀术"},  --腐蚀术
    DrainSoul = {id = 47855, name = GetSpellInfo(47855), tootip = "吸取灵魂"},  --吸取灵魂
    SoulLink = {id = 19028, name = GetSpellInfo(19028), tootip = "灵魂连接"},  --灵魂连接
    Immolate = {id = 47811, name = GetSpellInfo(47811), tootip = "献祭"},  --献祭

    DemonicCircleSummon = { id = 48018, name = GetSpellInfo(48018), tootip = "恶魔法阵"},

   
};


local enemies = {}

local function ActiveEnemies(range)
	table.wipe(enemies)
	enemies = ni.player.enemiesinrange(40)
	for k, v in ipairs(enemies) do
		if ni.player.threat(v.guid) == -1 then
			table.remove(enemies, k)
		end
	end
	return #enemies
end

local queue = {
    -- "Molten Armor",
    -- "IceBlock",
    -- "Arcane Brilliance",
    -- "Conjure Mana Gem",
    -- "Poly Focus",
	-- "Mana Gem",
    -- "Pause",
    -- "Pyroblast",
    -- "Combustion",
    -- "Living Bomb",
    -- "Fire Blastproc",
    -- -- "Living Bomb AOE",
    -- "BlastWave",
    -- "Flame Orb",
    -- "Flame Strike",
    -- "SorchMoving",
    -- "Dragon Breath",
    -- "Combustion",
    -- "Scorch",
    -- "Fireball",
    -- "Pyroblast",
    -- "LivingBomb",
    "Debug",
    "Pause",         -- 停止动作
    "Prepare",       -- 停止动作
    "Assist",        -- 闪现冰箱
    "KeepHealth",    -- 保命
    "KeepMana",      -- 回蓝
    "RemoveCurse",   -- 解诅咒
    "Spellsteal",    -- 偷魔法
    "AutoBurst",     -- 自动爆发
    "PolyFocus",     -- 焦点变羊
    "Pyroblast",     -- 炎爆术
    "BlizzardAoe",   -- 自动暴风雪
    "FlamestrikeAoe",   -- 自动烈焰风暴
    "LivingBombAoe", -- 群体炸弹
    "Scorch",        -- 灼烧
    "FireDps",       -- 折磨火输出
}

local lyfb = 42926;

local abilities = 
{
    ["Debug"] = function()
		local _, enebled = GetSetting("Debug")
        if enebled then
            ni.vars.debug = true
        else
            ni.vars.debug = false
        end
	end,

    ["Pause"] = function()
		if ni.tnr.pause() then
			return true
        end
	end,

    ["Prepare"] = function()


        if not ni.player.buff(spells.FelArmor.name) and ni.spell.available(spells.FelArmor.id) then -- 邪甲术
            ni.spell.cast(spells.FelArmor.name)
            return true
        end

        if ni.player.buffremaining(spells.LifeTap.name) < 3 and ni.spell.available(spells.LifeTap.id) then -- 生命分流
            ni.spell.cast(spells.LifeTap.name)
            return true
        end

        if ni.player.buffremaining(spells.LifeTap.name) < 3 and ni.spell.available(spells.LifeTap.id) then -- 武器附魔判断
            ni.spell.cast(spells.LifeTap.name)
            return true
        end


        if not ni.player.hasitem(41196) and ni.player.hasitem(6265) and not ni.player.ismoving() then -- 完美法术石
            ni.spell.cast(spells.CreateSpellstone.name)	
            return true
        end

        if not ni.player.hasitem(36895) and ni.player.hasitem(6265) and not ni.player.ismoving() then -- 灵魂石
            ni.spell.cast(spells.CreateSoulstone.name)	
            return true
        end

        if not ni.player.hasitem(36892) and ni.player.hasitem(6265) and not ni.player.ismoving() then -- 灵魂石
            ni.spell.cast(spells.CreateHealthstone.name)	
            return true
        end
        

        local _, enabled = GetSetting("EquipId")
        if enabled and not UnitAffectingCombat("player") and not ni.unit.buffs("player", "熔岩护甲||冰甲术||法师护甲") then
            local t9 = {47748, 47749, 47750, 47751, 47752, 47753, 47754, 47755, 47756, 47757, 47758, 47759, 47760, 47761, 47762};

            if ni.player.hassetequiped(t9, 2) then  --有2件套
                
            end

            -- 记录替换哪两个，替换回来？
            if not ni.player.hasitemequipped(51378) and ni.player.hasitem(51378) then 
							  
                ni.player.useitem(36892)
            end

            if not ni.player.hasitemequipped(51378) and ni.player.hasitem(51378) then
                ni.player.useitem(36892)
            end
        end

        -- 火盾
        if not ni.unit.buffs("player", "熔岩护甲||冰甲术||法师护甲") and ni.spell.available(spells.MoltenArmor.name) then
            ni.spell.cast(spells.MoltenArmor.name)
            return true
        end

        -- 奥术智慧
        if not ni.unit.buffs("player", "奥术智慧||奥术光辉||达拉然的光辉||达拉然智慧") and ni.spell.available(spells.ArcaneBrilliance.name) then
            ni.spell.cast(spells.ArcaneBrilliance.name)
            return true
        end

        -- 战斗状态不做石头
        if not UnitAffectingCombat("player") and not ni.player.ismoving() and not ni.player.hasitem(spells.ManaGem.name) then
            ni.spell.cast(spells.ConjureManaGem.name)
            return true
        end
	end,

    ["Assist"] = function()
        -- 闪现
		if ni.tnr.isleftshift() and ni.spell.available(spells.Blink.name) then
            ni.spell.cast(spells.Blink.name)
            return true
        end

        local value, enebled = GetSetting("UseHand")
        -- 工程手套
		if enebled then
            ni.tnr.useinventoryitem(10, value)
        end

        -- 工程鞋
		if ni.tnr.isrightctrl() then
            ni.tnr.useinventoryitem(8)
        end
	end,

    ["KeepHealth"] = function() -- 保命
        local stoneValue, stoneEnabled = GetSetting("FelHealthstone") 
        local shieldValue, shieldEnabled = GetSetting("ManaShield")
        local iceBlockValue, iceBlockEnabled = GetSetting("IceBlock")
        local tar = ni.objects["player"];

        -- 法力护盾
		if shieldEnabled and tar:exists() and tar:combat() and tar:hp() < shieldValue and ni.spell.available(spells.ManaShield.name) then
            ni.spell.cast(spells.ManaShield.name)
            return true
        end

        -- 吃糖
        if stoneEnabled and tar:exists() and tar:combat() and tar:hp() < stoneValue and ni.player.itemready(spells.FelHealthstone.name) then
            ni.player.useitem(spells.FelHealthstone.name) 
            return true
        end

        -- 使用冰箱
        if (ni.tnr.isrightshift() or (iceBlockEnabled and tar:exists() and tar:combat() and tar:hp() < iceBlockValue)) and ni.spell.available(spells.IceBlock.name) then
            ni.spell.cast(spells.IceBlock.name)
            return true
        end
	end,

    ["KeepMana"] = function() -- 回蓝
		local manaGemValue, manaGemEnabled = GetSetting("ManaGem")
        local evocationValue, evocationEnabled = GetSetting("Evocation")
        local tar = ni.objects["player"];

        if manaGemEnabled and tar:exists() and tar:combat() and tar:power() < manaGemValue and ni.player.itemready(spells.ManaGem.name) then
            ni.player.useitem(spells.ManaGem.name)
            return true
        end
        if evocationEnabled and tar:exists() and tar:combat() and tar:power() < evocationValue and ni.spell.available(spells.Evocation.name) then
            ni.spell.cast(spells.Evocation.name)
            return true
        end
	end,

    ["PolyFocus"] = function() -- 焦点变羊
        if ni.tnr.attack("focus") and ni.unit.debuffremaining("focus", spells.Polymorph.name) < 2 and ni.tnr.valid("focus", spells.Polymorph.name) then
            ni.spell.cast(spells.Polymorph.name, "focus")
            return true
        end
    end,

    ["RemoveCurse"] = function() -- 解诅咒
        local _, enabled = GetSetting("RemoveCurse")
        if enabled and ni.tnr.attack() then
            ni.tnr.helpmember(spells.RemoveCurse.name, "Curse")
        end
    end,
    
    ["Spellsteal"] = function() -- 偷魔法
        local _, enabled = GetSetting("Spellsteal")
        if enabled and ni.tnr.attack() then
            ni.tnr.removedebuff(spells.Spellsteal.name, "Magic")
        end
    end,

    
    ["AutoBurst"] = function() --爆发
        local _, enabled = GetSetting("AutoBurst")
        if (enabled and ni.unit.buffs("player", "嗜血||英勇||能量灌注")) or ni.tnr.isleftctrl() then
            
            -- 饰品
            ni.tnr.useinventoryitem(13) 
            ni.tnr.useinventoryitem(14)
            -- 燃烧
            if ni.spell.available(spells.Combustion.name) then
                ni.spell.cast(spells.Combustion.name)
            end

            -- 镜像
            if ni.spell.available(spells.MirrorImage.name) then
                ni.spell.cast(spells.MirrorImage.name)
                return true
            end
            
            -- 巨魔狂暴
            if ni.tnr.race() == "巨魔" then
                if ni.spell.available("狂暴") then
                    ni.spell.cast("狂暴")
                    return true
                end
            end

            -- 兽人血性狂怒
            if ni.tnr.race() == "兽人" then
                if ni.spell.available("血性狂怒") then
                    ni.spell.cast("血性狂怒")
                    return true
                end
            end
        end
	end,
    
    ["Pyroblast"] = function() --炎爆术
        local _, enabled = GetSetting("Pyroblast")
        if enabled and ni.tnr.attack() and ni.unit.buff("player", spells.HotStreak.name) and ni.tnr.valid("target", spells.Pyroblast.name) then
            ni.spell.cast(spells.Pyroblast.name)
            return true
        end
	end,

    ["BlizzardAoe"] = function() --暴风雪
        local aoeCount, enabled = GetSetting("BlizzardCount")
        
        if not ni.player.ismoving() and ni.tnr.isleftalt() or (enabled and ni.tnr.enemycount("target", 8) > aoeCount and ni.spell.available("target", spells.Flamestrike.name)) then
            ni.spell.castat(spells.Blizzard.name, "target")
            return true
        end
	end,

    ["FlamestrikeAoe"] = function() --烈焰风暴
        local aoeCount, enabled = GetSetting("FlamestrikeCount")
        if not ni.player.ismoving() and ni.tnr.isrightalt() or (enabled and ni.tnr.enemycount("target", 8) > aoeCount and ni.spell.available("target", spells.Flamestrike.name)) then
            local enemies = ni.unit.enemiesinrange("player", 30) 
            for i = 1, #enemies do
				local target = enemies[i].guid
                ni.spell.castat(lyfb,target)	
                return true
            end
        end
	end,

    ["LivingBombAoe"] = function() -- 群体炸弹
        local distance, enabled = GetSetting("LivingBomb")
        if enabled  then
            ni.tnr.aoecast(spells.LivingBomb.name , distance)
        end
	end,
	
    ["BlastWave"] = function() -- 冲击波
        if ni.vars.combat.aoe 
        and ni.spell.cd(spells.BlastWave.id)== 0
        and ActiveEnemies() > 2 then
            ni.spell.castat(spells.BlastWave.name,"target")
        end
    end,

    ["Flame Strike"] = function() -- 烈焰风暴
        if ni.vars.combat.aoe 
        and ActiveEnemies() > 2 then
            ni.spell.delaycast(spells.Flamestrike.name,"target", 8)
            ni.player.clickat("target")
        end
    end,

    ["Flame Orb"] = function() -- CTM/MOP技能
        if ni.spell.cd(spells.FlameOrb.id)== 0
        and ni.player.isfacing("player","target")
        then
            ni.spell.cast(spells.FlameOrb.name)
            return true
        end
    end,

    
	["Combustion"] = function() -- 燃烧
        local _, enabled = GetSetting("Combustion")
        if enabled
            and ni.vars.combat.cd
            and UnitAffectingCombat("player")
            and ni.spell.cd(spells.Combustion.id) == 0
            and not ni.unit.ischanneling("player")
            and ni.unit.debuff("target", 92315)
            and ni.unit.debuff("target", 44457)
            and ni.unit.debuff("target", 12654) then
                ni.spell.stopcasting()
                ni.spell.cast(26297) --Racial Troll
                ni.player.runtext("/use 13") -- Activable trinket 
                ni.player.useitem(58091) --Spell potion 
                ni.spell.cast(spells.Combustion.name, "target")
                return true;
        end
    end,

    ["Dragon Breath"] = function() --龙息术，暂时没用到
        if ni.spell.available(spells.DragonsBreath.id) 
        and ni.unit.inmelee("player", "target") then
            ni.spell.cast(spells.DragonsBreath.name)
        end
    end,

    ["Scorch"]= function() --灼烧
        local _, enabled = GetSetting("Scorch")
        if enabled and ni.tnr.attack() and ni.unit.isboss("target") and ni.tnr.valid("target", spells.Scorch.name) and not ni.unit.debuffs("target", "强化灼烧||暗影掌握") then -- Improved Scorch --
            ni.spell.delaycast(spells.Scorch.name, "target", 1.5)
            return true;
        end
    end,

    ["FireDps"] = function () --折磨火输出循环
        local _, enabled = GetSetting("FireDps")
        
        if enabled and ni.tnr.attack() then
            if ni.unit.buff("player", spells.HotStreak.name) and ni.tnr.valid("target", spells.Pyroblast.name) then
                ni.spell.cast(spells.Pyroblast.name)
            end
            if ni.unit.debuffremaining("target", spells.LivingBomb.name, "player") <= 0 and ni.tnr.valid("target", spells.LivingBomb.name, false) then -- 活动炸弹不用面对目标
                ni.spell.cast(spells.LivingBomb.name)
            end
            if not ni.player.ismoving() and ni.tnr.valid("target", spells.Fireball.name) then
                ni.spell.cast(spells.Fireball.name, "target")
            end
            if ni.tnr.valid("target", spells.FireBlast.name) then
                ni.spell.cast(spells.FireBlast.name)
            end
        end
    end,
} 

ni.bootstrap.profile("335痛苦术", queue, abilities, OnLoad, OnUnload);


-- Impact despues fireblast 
-- Check para Combustion 
--  92315 and ignite 1265
-- Check for a good ignite
